package com.telit.firecommand.service.impl;

import com.lowagie.text.Document;
import com.lowagie.text.DocumentException;
import com.lowagie.text.pdf.*;
import com.telit.firecommand.dao.CommFireReportDao;
import com.telit.firecommand.dto.CommFireReportDTO;
import com.telit.firecommand.entity.CommFireReport;
import com.telit.firecommand.service.FireReportService;
import com.telit.firecommand.vo.CommFireReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ClassPathResource;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * Create by Chencf 2023/02/13
 */
@Service
@Slf4j
public class FireReportServiceImpl implements FireReportService {
    @Autowired
    private CommFireReportDao commFireReportDao;




    @Override
    public CommFireReportVO findFireReport(String fireId) {
        CommFireReportVO fireReport = commFireReportDao.findFireReport(fireId);
        return fireReport;
    }


    @Override
    public void updateFireReport(CommFireReportDTO dto) {
        if (dto.getId()==null){
            //insert
            CommFireReport commFireReport = new CommFireReport();
            BeanUtils.copyProperties(dto,commFireReport);
            commFireReportDao.insertSelective(commFireReport);
        }else {
            commFireReportDao.updateDTO(dto);
        }
    }


    @Override
    public void printDetailPDF(String fireId, OutputStream os, HttpServletRequest request, HttpServletResponse response) throws IOException {

        //读取static下模板
        ClassPathResource classPathResource = new ClassPathResource("static/火情报告.pdf");
        InputStream inputStream = classPathResource.getInputStream();
        //生成的新文件路径  直接输出
        PdfReader reader;
        FileOutputStream out;
        ByteArrayOutputStream bos;
        PdfStamper stamper;
        try {
            reader = new PdfReader(inputStream);//读取pdf模板
            bos = new ByteArrayOutputStream();
            stamper = new PdfStamper(reader, bos);
            //获取pdf中的域
            AcroFields form = stamper.getAcroFields();
            BaseFont bf = BaseFont.createFont("fonts/STKAITI.TTF", BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
            form.addSubstitutionFont(bf);
            //根据火情id查询数据
            CommFireReportVO fireReport = commFireReportDao.findFireReport(fireId);
            //todo


            //封装标签
            Map<String, String> map = new HashMap<>();
           // map.put("projectName", projectName);
            int i = 0;
            //往标签域中填充数据
            java.util.Iterator<String> it = form.getFields().keySet().iterator();
            while (it.hasNext()) {
                String name = it.next().toString();
                form.setField(name, map.get(name));
                i++;
            }
            stamper.setFormFlattening(true);//如果为false那么生成的PDF文件还能编辑，一定要设为true
            stamper.close();
            Document doc = new Document();
            PdfCopy copy = new PdfCopy(doc, response.getOutputStream());
            PdfWriter.getInstance(doc, os);
            bos.close();
            doc.open();
            PdfImportedPage importPage = copy.getImportedPage(new PdfReader(bos.toByteArray()), 1);
            copy.addPage(importPage);
            doc.close();
            copy.close();
        } catch (IOException e) {
           log.error("下载pdf报错");
        } catch (DocumentException e) {
            log.error("下载pdf报错");
        }
    }
}
